//
//  UserManager.m
//  TianKeLong
//
//  Created by chenzhihui on 13-11-30.
//  Copyright (c) 2013年 青岛晨之辉信息服务有限公司. All rights reserved.
//

#import "UserManager.h"
@implementation UserManager
#pragma mark - 存取用户名
+(void)saveUsername:(NSString *)uname
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:uname forKey:@"username"];
}

+(NSString *)username
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"username"];
}

#pragma mark - 存取密码
+(void)savePassword:(NSString *)password
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:password forKey:@"password"];
}

+(NSString *)password
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"password"];
}

#pragma mark - 存取用户名
+(void)saveRealname:(NSString *)realname
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:realname forKey:@"realname"];
}

+(NSString *)realname
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"realname"];
}
#pragma mark - 存、取语言
+(void)saveLanguage:(NSString *)language
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:language forKey:@"language"];
    
}
+(NSString *)language
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"language"];
}
#pragma mark - 存、取微站地址
+(void)saveWeiUrl:(NSString *)url
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:url forKey:@"weiUrl"];

}
+(NSString *)weiUrl
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"weiUrl"];

}


#pragma mark - 存取记住密码状态
+(void)saveState:(NSString *)state
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:state forKey:@"state"];
}

+(NSString *)state
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"state"];
}

#pragma mark - 存取key
+(void)saveKey:(NSString *)key
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:key forKey:@"key"];
}

+(NSString *)key
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"key"];
}

#pragma mark - 存、取帐号推送状态(1:推送；0无推送)
+(void)savePushType:(NSString*)isPush
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:isPush forKey:@"isPush"];
}
+(BOOL)isPush
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString* type = [defaults stringForKey:@"isPush"];
    BOOL isPush = [type boolValue];
    return isPush;
}


#pragma mark - 存、取录音权限
+(void)saveRecordType:(NSString *)record
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:record forKey:@"isRecord"];
}
+(BOOL)isRecord
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString* type = [defaults stringForKey:@"isRecord"];
    BOOL isRecord = [type boolValue];
    return isRecord;
   
}

#pragma mark - 存、取主帐号子帐号权限
+(void)saveChild:(NSString *)child
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:child forKey:@"isChild"];
}

+(NSString *)child
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"isChild"];
}



#pragma mark - 存、取公司名
+(void)saveCompanyname:(NSString *)name
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:name forKey:@"company"];
}
+(NSString *)companyname
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"company"];
}

#pragma mark - 存、取帐号id
+(void)saveMemberId:(NSString *)member_id
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:member_id forKey:@"member_id"];
}

+(NSString *)member_id
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"member_id"];
}

//存、取主帐号id
+(void)saveMasterId:(NSString *)master_id
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:master_id forKey:@"master_id"];
}
+(NSString *)master_id
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults stringForKey:@"master_id"];
}

//存、取是否是新版本
+(void)hasNewVersion:(NSString*)version
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:version forKey:@"newVersion"];
}
+(BOOL)isNewVersion
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * str = [defaults stringForKey:@"newVersion"];
    if ([str isEqualToString:@"1"]) {
        return YES;
    }else{
        return NO;
    }
}

//存、取是否登录过用于统计
+(void)saveLoginTypeForEffect:(NSString*)isLogin
{
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:isLogin forKey:@"isLoginForEffect"];
}

+(BOOL)isLoginTypeForEffect
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * str = [defaults stringForKey:@"isLoginForEffect"];
    if ([str isEqualToString:@"1"]) {
        return YES;
    }else{
        return NO;
    }
}

//存、取是否登录过
+(void)saveLoginType:(NSString*)isLogin
{
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:isLogin forKey:@"isLogin"];
}

+(BOOL)isLoginType
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * str = [defaults stringForKey:@"isLogin"];
    if ([str isEqualToString:@"1"]) {
        return YES;
    }else{
        return NO;
    }
}

#pragma mark - 存取操作权限
+(void)saveJurisdiction:(NSDictionary *)jurisdiction
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:jurisdiction forKey:@"jurisdiction"];
}

+(NSDictionary *)jurisdiction
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults dictionaryForKey:@"jurisdiction"];
}
#pragma mark - 判断登录状态
+(BOOL)isLogin
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    
    @try {
        NSString * key = [defaults stringForKey:@"key"];
        if ([key length] > 0) {
            return YES;
        }
    }
    @catch (NSException *exception) {
        return NO;
    }
    return NO;
}

#pragma mark - 退出登录清除记录
+(void)logoff
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"key"];
}

#pragma mark - 正则验证
+(BOOL)regex_match:(NSString *)regexStr andValue:(NSString *)value
{
    NSError *error;
    //http+:[^\\s]* 这是检测网址的正则表达式
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];
    
    if (regex != nil) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:value options:0 range:NSMakeRange(0, [value length])];
        
        if (firstMatch) {
            //            NSRange resultRange = [firstMatch rangeAtIndex:0];
            //            //从urlString中截取数据
            //            NSString *result = [value substringWithRange:resultRange];
            //            NSLog(@"%@",result);
            return YES;
        }
    }
    return NO;
}

@end
